package leetCode.offer66;


import java.util.Arrays;

public class Solution3 implements Solution{


    /**
     * 不使用除法的解法
     * @param a
     * @return
     */
    @Override
    public int[] constructArr(int[] a) {
        if(a==null||a.length==0) return new int[0];
        if(a.length==1) return a;
        int[] preArr = new int[a.length-1];
        int[] afterArr = new int[a.length-1];
        int[] result = new int[a.length];
        int length = afterArr.length;
        int preMul = 1;
        int afterMul = 1;
        for(int i=0;i<a.length-1;i++){
            preMul *= a[i];
            preArr[i] = preMul;
            afterMul *= a[length--];
            afterArr[i] = afterMul;
        }
        result[0] = afterArr[afterArr.length-1];
        result[a.length-1] = preArr[preArr.length-1];
        for(int i=1;i<a.length-1;i++){
            result[i] = preArr[i-1] * afterArr[afterArr.length-i-1];
        }
        return result;
    }

    public static void main(String[] args) {
        Solution3 solution3 = new Solution3();
        int[] param = {1,2,3,4,5};
        System.out.println(Arrays.toString(solution3.constructArr(param)));
    }
}
